{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14404\n",
      "3\n",
      "(3, 14404)\n"
     ]
    }
   ],
   "source": [
    "f = open(\"20201124.XYZ\")\n",
    "lines = f.readlines()\n",
    "f.close()\n",
    "\n",
    "def parseXYZ(lines):\n",
    "    rawdata = []\n",
    "    for ele in lines:\n",
    "        ele = ele.strip().split(\" \")\n",
    "        x = [i for i in ele if len(str(i)) > 0]\n",
    "        if len(x) != 3:\n",
    "            continue\n",
    "        rawdata.append(x)\n",
    "    return rawdata\n",
    "rawdata = parseXYZ(lines)\n",
    "print(len(rawdata))\n",
    "print(len(rawdata[0]))\n",
    "\n",
    "import numpy as np\n",
    "npdata = np.array(rawdata)\n",
    "npdata = npdata.T\n",
    "print(npdata.shape)\n",
    "X, Y = np.meshgrid(npdata[0][0:30], npdata[1][0:30])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using matplotlib backend: Qt5Agg\n"
     ]
    }
   ],
   "source": [
    "%matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "plt.contourf(npdata, 20)\n",
    "# plt.contour(X,Y, npdata[2])\n",
    "# plt.clabel(X, inline=True, fontsize=12)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10, 10)\n"
     ]
    }
   ],
   "source": [
    "x = np.linspace(-3, 3, 10)\n",
    "y = np.linspace(-3, 3, 10)\n",
    "X,Y = np.meshgrid(x, y)\n",
    "print(X.shape)\n",
    "def f(x, y):\n",
    "    return (1-x/2+x**5+y**3) * np.exp(-x**2-y**2)\n",
    "plt.contourf(X,Y, f(X,Y))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loadFile(filename=''):\n",
    "    \"\"\"\n",
    "    Load pickle format file\n",
    "\n",
    "    :param filename: the filename, if the filename is not the full path, the program will search it in default data directory\n",
    "    :return: the data loaded\n",
    "    :return: errorhandle.UNKNOWN_FILE_FORMAT if the file suffix is not end with .pkl or npy\n",
    "    :return: errorhandle.LOAD_FILE_IO_ERROR if an exception occurs while loading file\n",
    "    \"\"\"\n",
    "    import pickle\n",
    "    import numpy as np\n",
    "    f = \"\"\n",
    "    if not filename:\n",
    "        return \"errorhandle.UNKNOWN_FILE_NAME\"\n",
    "    elif len(filename.split('/')) > 1:\n",
    "        filepath = filename\n",
    "        fileformat = filename.split('/')[-1].split('.')[1]\n",
    "    else:\n",
    "        fileformat = filename.split('.')[1]\n",
    "        filepath = \"./\"\n",
    "    try:\n",
    "        f = open(filepath, 'rb')\n",
    "        if fileformat == 'pkl':\n",
    "            return pickle.load(f)\n",
    "        elif fileformat == 'npy':\n",
    "            return np.load(f)\n",
    "        else:\n",
    "            return \"errorhandle.UNKNOWN_FILE_FORMAT\"\n",
    "    except IOError as e:\n",
    "        print(e)\n",
    "        return \"errorhandle.LOAD_FILE_IO_ERROR\"\n",
    "    finally:\n",
    "        if f:\n",
    "            f.close()\n",
    "\n",
    "import numpy as np\n",
    "def toNP(CH1, lla):\n",
    "    ch1 = np.asarray(CH1)\n",
    "    latLonAlt = np.asarray(lla)\n",
    "    latLonAlt = latLonAlt.T\n",
    "    lat = latLonAlt[1]\n",
    "    lon = latLonAlt[0]\n",
    "    print(len(CH1))\n",
    "    print(np.mean(ch1))\n",
    "    print(np.mean(lat))\n",
    "    print(np.mean(lon))\n",
    "    return ch1, lat, lon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "93525\n",
      "45920.02834616699\n",
      "23.199749460484693\n",
      "113.49626227447511\n",
      "93631\n",
      "45912.96675202801\n",
      "23.199749509017906\n",
      "113.49626221877189\n"
     ]
    }
   ],
   "source": [
    "CH1 = loadFile(\"debug_resource/2021_03_11_15_51_30_CX1.pkl\")\n",
    "lla = loadFile(\"debug_resource/2021_03_11_15_51_30_lla.pkl\")\n",
    "res = toNP(CH1, lla)\n",
    "CH1 = loadFile(\"debug_resource/2021_03_11_16_12_32_CX1.pkl\")\n",
    "lla = loadFile(\"debug_resource/2021_03_11_16_12_32_lla.pkl\")\n",
    "res = toNP(CH1, lla)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "93548\n",
      "45922.18838841014\n",
      "23.19974972679464\n",
      "113.49626202150995\n"
     ]
    }
   ],
   "source": [
    "CH1 = loadFile(\"debug_resource/2021_03_11_16_00_57_CX1.pkl\")\n",
    "lla = loadFile(\"debug_resource/2021_03_11_16_00_57_lla.pkl\")\n",
    "res = toNP(CH1, lla)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 庞老师数据量以及平均值\n",
    "# 93743\n",
    "# 45921.00182049734\n",
    "# 23.199749579776356\n",
    "# 113.49626215405877"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
